﻿using com.iot.core.annotation;
using com.iot.core.boot;
using com.iot.core.boot.configuration;
using com.iot.core.data;
using com.iot.core.data.entity;
using com.iot.core.exception;
using com.iot.core.infrastructure;
using com.iot.core.manager.auth;
using com.iot.core.manager.htcls;
using com.iot.core.manager.id;
using com.iot.core.util;
using com.iot.ht.Controllers.htuser.data;
using com.iot.ht.Controllers.htuser.service;
using Google.Protobuf.WellKnownTypes;
using Lazy.Captcha.Core;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace com.iot.ht.Controllers.htlogin
{
    [ApiController]
    [Route("[controller]/[action]")]
    [Tag(Name = "登录", Description = "登录及获取个人信息")]
    public class HtLoginController : ControllerBase
    {
        private readonly ICaptcha _ICaptcha;
        private readonly IHtUserService htUserService;
        
        public HtLoginController(ICaptcha p1, IHtUserService p2)
        {
            _ICaptcha = p1;
            htUserService = p2;




            //var minit = new DbInit();
            // minit.Init();
            //minit.AddUser(1000L, "htadmin", "后台管理", "111111", 1000L, 1000L);
            //minit.SaveChanges();

            //var minit = new DbInit();
            //var rowno = 0;
            //for(var i= 10000; i <= 14000; i++)
            //{
            //    var id = i;
                
            //    minit.AddUser(i, "ht"+i.ToString(), "ht" + i.ToString(), "111111", 1000L, 1000L);
            //    rowno++;
            //    if(rowno%100==0)
            //    {
            //        minit.SaveChanges();
            //    }
                
            //}
        }


        [HttpGet]
        [Operation(Summary= "验证码", Description ="" )]
        [Auth(AuthType.None)]
        [Log(LogNeed = false, LogPin = false, LogPout = false, LogTemplete = "")]
        public IActionResult Captcha([FromQuery] string Id)
        { 
            var info = _ICaptcha.Generate(Id);
            var stream = new MemoryStream(info.Bytes);             
            return File(stream, "image/gif");
        }


        [HttpPost]
        [Operation(Summary = "登录", Description = "")]
        [Auth(AuthType.None)]
        [Log(LogNeed = false, LogPin = false, LogPout = false, LogTemplete = "")]
        public async Task<string> Login(LoginDto mm)
        {
            var projectInfoProperty = Singleton<AppSettings>.Instance.Get<ProjectCfg>();
            if(!projectInfoProperty.DebugMode)
            {
                var bSuccess = _ICaptcha.Validate(mm.YzmId, mm.YzmValue, true, true);
                if (!bSuccess)
                {
                    throw new MyException("验证码错误");
                }
            } 
            return await AuthManager.Current.LoginAsync(mm);
        }


        [HttpPost]
        [Operation(Summary = "获取用户机构", Description = "")]
        [Auth(AuthType.None)]
        [Log(LogNeed = false, LogPin = false, LogPout = false, LogTemplete = "")]
        public async Task<List<HtDeptBaseDto>> GetDept(LoginDto mm)
        {
            var m2 = new LoginUserPwdDto
            {
                Ename = mm.Ename,
                Id = 0L,
                Pwd = mm.Pwd,
            };
            return await AuthManager.Current.LoginCheckAsync(m2);
        }



        [HttpPost]
        [Operation(Summary = "获取个人信息", Description = "")]
        [Auth(AuthType.Login)]
        [Log(LogNeed = false, LogPin = false, LogPout = false, LogTemplete = "")]
        public async Task<HtUserDto> GetUserInfo()
        {
            var myContext = EngineContext.Current.Resolve<MyContext>();
            var su = myContext.Su;

            var m2 = new HtUserDetailQto
            {
                Id=su.CrtUid,
                HtdeptId=su.HtdeptId
            };
            var m =await htUserService.DetailAsync(m2);
            return m;
        }


        [HttpPost]
        [Operation(Summary = "获取个人菜单", Description = "")]
        [Auth(AuthType.Login)]
        [Log(LogNeed = false, LogPin = false, LogPout = false, LogTemplete = "")]
        public async Task<List<TreeViewNode>> GetMenu()
        {
            return await AuthManager.Current.GetMenuAsync();
        }






    }
}
